package org.apache.spark

import org.apache.spark.rpc.{RpcEndpoint, RpcEnv}
import org.apache.spark.sql.SparkSession

object RpcServerMain {

  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
    val sparkSession = SparkSession.builder().config(conf).master("local[*]").appName("NX RPC").getOrCreate()

    val sparkContext:SparkContext = sparkSession.sparkContext
    val sparkEnv:SparkEnv= sparkContext.env

    /**
     * rpcEnv 相当于 akka中的akkaSystem
     */
    val rpcEnv: RpcEnv = RpcEnv.create(HelloRpcSettings.getName(),
      HelloRpcSettings.getHostname(),
      HelloRpcSettings.getHostname(),
      HelloRpcSettings.getPort(),
      conf,
      sparkEnv.securityManager, 1, false)

    //Endpoint 相当于akka中的一个Actor通信组件
    val helloEndPoint :HelloEndPoint = new HelloEndPoint(rpcEnv)
    rpcEnv.setupEndpoint(HelloRpcSettings.getName(),helloEndPoint);
    rpcEnv.awaitTermination()
  }

}
